﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace PokemonRuby
{
    public class MinorCharacter : MoveableObject
    {
        public List<Image> lstImg = new List<Image>();
        public int movingStatus = 0;
        public int movingDirection = 0;
        public int x = 9;
        public int y = 12;
        public int z = 0;
        public static Hashtable htMinorCharacter = new Hashtable();
        public Hashtable htMessage = new Hashtable();
        public string name;

        protected static string appPath = null;

        public MinorCharacter() {
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            appPath = fi.Directory.FullName;
        }
        public static void LoadMinorCharacter()
        {
            htMinorCharacter.Clear();
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            appPath = fi.Directory.FullName;
            string path = Path.Combine(appPath, "CharacterImages");
            string[] sar = Directory.GetFiles(path, "*.png");
            for (int i = 0; i < sar.Length; i++)
            {
                string fileName = sar[i];
                fi = new FileInfo(fileName);

                string key = fi.Name;
                key = key.Substring(0, key.Length - 4);
                key = key.ToLower().Trim();

                Image im = Image.FromFile(fileName);
                htMinorCharacter.Add(int.Parse(key), im);
            }
        }
        public MinorCharacter CreateMinorCharacter(MinorCharacter minor, int hinhBatDau, int hinhKetThuc, int x, int y, int z)
        {
            //MinorCharacter minor = new MinorCharacter();
            for (int i = hinhBatDau; i <= hinhKetThuc; i++)
            {
                minor.lstImg.Add((Image)htMinorCharacter[i]);
            }
            minor.x = x;
            minor.y = y;
            minor.z = z;
            movingDirection = 1;
            return minor;
        }
        
        public override bool IdleAction(Map map)
        {
            Random rd = new Random();
            int kq = rd.Next(0, 4);

            if (kq == 0)
            {
                if (map.MAP[x + 1, y, z] > 100000)
                {
                    return false;
                }
                else
                {
                    this.x = x + 1;
                    if (x > 11)
                    {
                        x = x - 1;
                        return false;
                    }
                        
                    movingDirection = 1;
                }
                
            }
            else if (kq == 1)
            {

                if (map.MAP[x - 1, y, z] > 100000)
                {
                    return false;
                }
                else
                {
                    this.x = x - 1;
                    if (x > 11)
                    {
                        x = x - 1;
                        return false;
                    }
                        
                    movingDirection = 0;
                }
            }
            else if (kq == 2)
            {

                if (map.MAP[x, y + 1, z] > 100000)
                {
                    return false;
                }
                else
                {
                    this.y = y + 1;
                    if (y > 14)
                    {
                        y = y - 1;
                        return false;
                    }
                       
                    movingDirection = 3;
                }
            }
            else
            {

                if (map.MAP[x, y - 1, z] > 100000)
                {
                    return false;
                }
                else
                {
                    this.y = y - 1;
                    if (y < 10) 
                    {
                        y = y + 1;
                        return false;
                    }
                        
                    movingDirection = 2;
                }
            }
            
            return true;
        }
    }
}
